package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import com.wyzz.hr.action.SalaryConfigAssist;
import com.wyzz.hr.base.Constants;

import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class ExportSalaryConfig extends MutipleSheetExportHandler implements
		Constants {

	public ExportSalaryConfig(List results[], List titles[]) {
		super(results, titles);
		sheetTitle = new ArrayList();
	}

	public ExportSalaryConfig(Map resultMap) {
		this(null, null);
		this.resultMap = resultMap;
		String key;
		for (Iterator it = resultMap.keySet().iterator(); it.hasNext(); sheetTitle
				.add(key))
			key = (String) it.next();

	}

	protected void buildTitle() throws RowsExceededException, WriteException {
		int size = resultMap.keySet().size();
		writableSheets = new WritableSheet[size];
		for (int i = 0; i < size; i++) {
			writableSheets[i] = writableWorkbook.createSheet(
					(String) sheetTitle.get(i), i);
			writableSheets[i].setRowView(0, 500);
			writableSheets[i].setColumnView(2, 15);
			writableSheets[i].setColumnView(6, 20);
			writableSheets[i].setColumnView(8, 15);
			writableSheets[i].setColumnView(10, 15);
			int sheetTitleLength = ((List[]) resultMap.get(sheetTitle.get(i)))[0]
					.size();
			for (int j = 0; j < sheetTitleLength; j++) {
				if (j > 10)
					writableSheets[i].setColumnView(j, 20);
				writableSheets[i].addCell(new Label(j, 0,
						(String) ((List[]) resultMap.get(sheetTitle.get(i)))[0]
								.get(j), titleCellFormat));
			}

		}

	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		int i = 0;
		for (Iterator i$ = resultMap.keySet().iterator(); i$.hasNext();) {
			String key = (String) i$.next();
			List contents = ((List[]) resultMap.get(key))[1];
			int j = 0;
			for (Iterator $ = contents.iterator(); $.hasNext();) {
				SalaryConfigAssist salaryConfigAssist = (SalaryConfigAssist) $
						.next();
				writableSheets[i].setRowView(j + 1, 400);
				writableSheets[i].addCell(new Label(0, j + 1,
						salaryConfigAssist.getEmpNo(), defaultCellFormat));
				writableSheets[i].addCell(new Label(1, j + 1,
						salaryConfigAssist.getEmpName(), defaultCellFormat));
				writableSheets[i].addCell(new Label(2, j + 1,
						salaryConfigAssist.getEmpDept(), defaultCellFormat));
				writableSheets[i].addCell(new Label(3, j + 1,
						salaryConfigAssist.getEmpType(), defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(4, j + 1, salaryConfigAssist
								.getEmpLocation(), defaultCellFormat));
				writableSheets[i].addCell(new Label(5, j + 1,
						salaryConfigAssist.getEmpStatus().toString(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(6, j + 1,
						salaryConfigAssist.getEmpCount(), defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(7, j + 1, salaryConfigAssist
								.getEmpBankName(), defaultCellFormat));
				writableSheets[i].addCell(new Label(8, j + 1,
						salaryConfigAssist.getCostCenter(), defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(9, j + 1, salaryConfigAssist
								.getSalaryLevel(), defaultCellFormat));
				writableSheets[i].addCell(new Label(10, j + 1,
						salaryConfigAssist.getAcceptName(), defaultCellFormat));
				for (int acssist = 0; acssist < salaryConfigAssist.getList()
						.size(); acssist++) {
					Number nb = new Number(11 + acssist, j + 1,
							getFloatValue((BigDecimal) salaryConfigAssist
									.getList().get(acssist)), numberCellFormat);
					writableSheets[i].addCell(nb);
				}

				j++;
			}

			i++;
		}

		writableWorkbook.write();
	}

	private float getFloatValue(BigDecimal bd) {
		float fl = 0.0F;
		if (bd != null)
			fl = bd.floatValue();
		return fl;
	}

	private Map resultMap;
	private List sheetTitle;
}
